<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-tabs>
      <el-tab-pane label="个人详情">
        <template>
          <div class="app-container">
            <!-- 个人信息 -->
            <el-form ref="from" label-width="100px" :model="formData" :rules="rules">
              <!-- 姓名 -->
              <el-row class="inline-info">
                <el-col :span="12">
                  <el-form-item label="姓名" prop="departmentName">
                    <el-input v-model="formData.username" />
                  </el-form-item>
                </el-col>
              </el-row>
              <!-- 部门 -->
              <el-row class="inline-info">
                <el-col :span="12">
                  <el-form-item label="部门" prop="departmentName">
                    <el-input v-model="formData.departmentName" />
                  </el-form-item>
                </el-col>
              </el-row>
              <!-- 入职时间 -->
              <el-row class="inline-info">
                <el-col :span="12">
                  <el-form-item label="入职时间">
                    <el-date-picker
                      v-model="formData.correctionTime"
                      type="date"
                      class="inputW"
                      value-format="yyyy-MM-dd"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <!-- //手机号 -->
              <el-row class="inline-info">
                <el-col :span="12">
                  <el-form-item label="手机号">
                    <el-input
                      v-model="formData.mobile"
                      disabled
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <!-- 员工照片 -->
              <el-row class="inline-info">
                <el-col :span="12">
                  <el-form-item label="员工头像">
                    <!-- 放置上传图片 -->
                    <UploadImg :form-data="formData" @change-userPhoto="changeuserPhotoFn" />
                  </el-form-item>
                </el-col>
              </el-row>
              <!-- 保存个人信息 -->
              <el-row class="inline-info" type="flex" justify="center">
                <el-col :span="12">
                  <el-button type="primary" @click="newFn">保存更新</el-button>
                  <el-button @click="$router.back()">返回</el-button>
                </el-col>
              </el-row>
            </el-form>

          </div>
        </template>
        <el-tab-pane /></el-tab-pane></el-tabs>
  </el-card>

</template>

<script>
import { getUserDetailByIdAPI, saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      formData: {},
      rules: {
        departmentName: [
          { required: true, message: '请输入信息', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getMessage()
  },
  methods: {
    // 改变头像,xx：头像云地址
    changeuserPhotoFn(xx) {
      if (!this.formData.staffPhoto) {
        this.formData.staffPhoto = ''
      }
      this.formData.staffPhoto = xx
    },
    // 获取员工详情
    async getMessage() {
      const res = await getUserDetailByIdAPI(this.$route.params.id)
      this.formData = res
      console.log('员工详情', res)
    },
    newFn() {
      this.$refs.from.validate(async() => {
        await saveUserDetailById(this.formData)
        this.$message.success('修改成功')
        this.getMessage()
        if (this.formData.id === this.$store.state.user.user_message.id) {
          this.$store.dispatch('user/setUserMessageActions')
          console.log(this.$store.state.user.user_message)
        }
      })
    }
  }

}
</script>

<style lang="scss" scoped>

</style>
